#!/usr/bin/env python
#
# Copyright (C) 2009 Dody Suria Wijaya.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__author__ = 'dodysw@gmail.com (Dody Suria Wijaya)'

import unittest
import ajaxfeed
import ajaxfeed.client
from ajaxfeed import test_data

class LoadFeedTest(unittest.TestCase):

    def testLoadActual(self):
        #this feed hasn't change since 2003, so it's a good bet as test source
        source_uri = 'http://cyber.law.harvard.edu/rss/examples/rss2sample.xml'
        client = ajaxfeed.client.AjaxFeedClient()
        feed = client.GetLoadFeed(source_uri)
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.title.text, 'Liftoff News')
        self.assertEqual(feed.link.text,
                        'http://liftoff.msfc.nasa.gov/')
        self.assertEqual(feed.description.text,
                        'Liftoff to Space Exploration.')
        self.assertEqual(len(feed.entries), 4)
        self.assertEqual(feed.entries[0].title.text, 'Star City')

    def testLoadFromString(self):
        feed = ajaxfeed.GAjaxFeedLoadFromString(test_data.LOAD_FEED_JSON)
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.title.text, 'Liftoff News')
        self.assertEqual(feed.link.text,
                        'http://liftoff.msfc.nasa.gov/')
        self.assertEqual(feed.description.text,
                        'Liftoff to Space Exploration.')
        self.assertEqual(len(feed.entries), 4)
        self.assertEqual(feed.entries[0].title.text, 'Star City')

    def testLoadWasAtomFromString(self):
        feed = ajaxfeed.GAjaxFeedLoadFromString(
                                            test_data.LOAD_FEED_JSON_WAS_ATOM)
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.title.text, 'Atom')
        self.assertEqual(feed.link.text,'http://atompub.org')
        self.assertEqual(feed.description.text, 'Atom news in Atom.')
        self.assertEqual(len(feed.entries), 1)
        self.assertEqual(feed.entries[0].title.text, 'Atom 1.0')


class LookupFeedTest(unittest.TestCase):
    def testLookupActual(self):
        client = ajaxfeed.client.AjaxFeedClient()
        feed = client.GetLookupFeed('http://www.kompas.com')
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.query.text, 'http://www.kompas.com')
        self.assertEqual(feed.url.text, 'http://www.kompas.com/getrss/all')

    def testLookupFromString(self):
        feed = ajaxfeed.GAjaxFeedLookupFromString(test_data.LOOKUP_FEED_JSON)
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.query.text, 'http://www.kompas.com')
        self.assertEqual(feed.url.text, 'http://www.kompas.com/getrss/all')

class FindFeedTest(unittest.TestCase):
    def testFindActual(self):
        client = ajaxfeed.client.AjaxFeedClient()
        feed = client.GetFindFeed('kompas')
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.query.text, 'kompas')
        filtered = [entry for entry in feed.entries
            if entry.link.text == 'http://www.kompas.com/']
        self.assertEqual(len(filtered), 1)
        kompas_entry = filtered[0]
        self.assertEqual(kompas_entry.url.text,
                        'http://www.kompas.com/getrss/all')

    def testFindFromString(self):
        feed = ajaxfeed.GAjaxFeedFindFromString(test_data.FIND_FEED_JSON)
        self.assertEqual(feed.responseStatus, 200)
        self.assertEqual(feed.query.text, 'kompas')
        filtered = [entry for entry in feed.entries
            if entry.link.text == 'http://www.kompas.com/']
        self.assertEqual(len(filtered), 1)
        kompas_entry = filtered[0]
        self.assertEqual(kompas_entry.url.text,
                        'http://www.kompas.com/getrss/all')

if __name__ == '__main__':
  unittest.main()
